package com.atguigu.yygh.handler;

import com.atguigu.yygh.exception.YyghException;
import com.atguigu.yygh.result.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 统一异常处理类
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public R error(Exception e){
        e.printStackTrace();
        log.error("全局异常："+e.getLocalizedMessage());
        return R.error();
    }
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public R error(ArithmeticException e){
        e.printStackTrace();
        log.error("特定异常："+e.getLocalizedMessage());
        return R.error().message("执行了特定异常");
    }

    @ExceptionHandler(YyghException.class)
    @ResponseBody
    public R error(YyghException e){
        e.printStackTrace();
        log.error("YyghException异常："+e.getLocalizedMessage());
        return R.error().message(e.getMsg()).code(e.getCode());
    }
}